/*
* $Id: Setting.cpp 143 2010-09-09 19:55:18Z volok-aleksej@yandex.ru $
*
* Copyright 2008 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "Setting.h"

CSettings::CSettings(int nIdString, const std::tstring& strRegName, bool bHeader)
: m_nNameSettingsId(nIdString), m_bHeader(bHeader), m_strRegName(strRegName){}
CSettings::~CSettings(){}

void CSettings::Init(){
	std::map<std::tstring, std::tstring> aStates = GetStates();
	std::map<std::tstring, std::tstring> aDefaultStates = GetDefaultStates();
	std::map<std::tstring, std::vector<Parameter> > aParameter = GetParameters();
	std::vector<Window> aWindow = GetWindows();
	for(std::vector<Window>::iterator it = aWindow.begin(); it != aWindow.end(); it++)
		m_aWindows.insert(m_aWindows.end(), std::make_pair(sWindow(*it, aDefaultStates[it->m_strId], aParameter[it->m_strId]), aStates[it->m_strId]));
}
std::tstring CSettings::GetNameSetting(){
	return ILanguageManager::GetLanguageManager()->GetString(m_nNameSettingsId);
}
bool CSettings::IsHeader(){
	return m_bHeader;
}
std::vector<sWindow*> CSettings::GetDynamicInterface(){
	std::vector<sWindow*> aWindows;
	for(std::map<sWindow, std::tstring>::iterator it = m_aWindows.begin(); it != m_aWindows.end(); it++){
		std::tstring strState = it->second;
		if(strState.empty())
			strState = it->first.m_strState;
		for(std::map<std::tstring, std::tstring>::iterator it1 = m_aChange.begin(); it1 != m_aChange.end(); it1++){
			if(it1->first == it->first.m_window.m_strId){
				strState = it1->second;
				break;
			}
		}
		aWindows.push_back(new sWindow(it->first.m_window, strState, it->first.m_aParameters));
	}
	return aWindows;
}
void CSettings::SetState(const std::tstring& strId, const std::tstring& strState){
	std::map<std::tstring, std::tstring>::iterator it = m_aChange.find(strId);
	if(it != m_aChange.end())
		it->second = strState;
	else
		m_aChange.insert(m_aChange.end(), std::make_pair(strId, strState));
}
std::tstring CSettings::GetState(const std::tstring& strId){
	if(m_aChange.find(strId) != m_aChange.end())
		return m_aChange[strId];
	else
		return GetCurrentValue(strId);
}
void CSettings::SaveSetting(){
	for(std::map<std::tstring, std::tstring>::iterator it = m_aChange.begin(); it != m_aChange.end(); it++){
		bool bSave;
		std::tstring saveValue = GetSaveValue(it->first, it->second, bSave);
		if(bSave)
			CCommonFunction::SetRegValue(m_strRegName, it->first, saveValue);
		for(std::map<sWindow, std::tstring>::iterator it1 = m_aWindows.begin(); it1 != m_aWindows.end(); it1++){
			if(it1->first == it->first){
				it1->second = it->second;
				break;
			}
		}
	}
	if(IsChanged())
		PostSave();
	CancelChange();
}
void CSettings::CancelChange(){
	m_aChange.clear();
}
void CSettings::DefaultSetting(){
	m_aChange = GetDefaultStates();
}
bool CSettings::IsChanged(){
	return !m_aChange.empty();
}
std::tstring CSettings::GetCurrentValue(const std::tstring& strId){
	std::tstring strValue;
	for (std::map<sWindow, std::tstring>::iterator it = m_aWindows.begin(); it != m_aWindows.end(); it++){
		if(it->first == strId){
			strValue = it->second;
			if(strValue.empty())
				strValue = it->first.m_strState;
			break;
		}
	}
	return strValue;
}
std::tstring CSettings::GetDefaultValue(const std::tstring& strId){
	std::map<std::tstring, std::tstring> aStates = GetDefaultStates();
	return aStates[strId];
}
void CSettings::PostSave(){}
std::tstring CSettings::GetSaveValue(const std::tstring& strValueName, const std::tstring& strValue, bool& bSave){
	bSave = TRUE;
	return strValue;
}
void CSettings::Free(){
	delete this;
}